package ylg.com.cn.beuty.main.fragment.fragment_home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.haitao.common.utils.PixelUtil;

import de.greenrobot.event.EventBus;
import ylg.com.cn.beuty.R;
import ylg.com.cn.beuty.inter.UserEvent;
import ylg.com.cn.beuty.main.adapter.MyPagerAdapter;
import ylg.com.cn.beuty.utils.Tools;

/**
 * Created by MyPC on 2017/1/13.
 */
public class HomeFragement extends Fragment implements View.OnClickListener {

    private View rootView;
    private VideoFrament videoFragment;//视频
    private AttentionFragement catFragment;//关注

    //屏幕的宽度

    private TextView textView[] = new TextView[2];
    private int index = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        rootView  = inflater.inflate(R.layout.fragment_home, container, false);
        findView();
        init();
        initLine();

        return rootView;
    }

    @Override
    public void onClick(View v) {

    }

    private LinearLayout mLin_expert,mLin_cat;
    private View mLine;
    private ViewPager mViewPager;
    private TextView mTv_expert,mTv_cat;
    private void findView(){
        mLin_expert = (LinearLayout) rootView.findViewById(R.id.Lin_expert);
        mLin_expert.setOnClickListener(this);
        mLin_cat = (LinearLayout) rootView.findViewById(R.id.Lin_cat);
        mLin_cat.setOnClickListener(this);
        mLine = (View) rootView.findViewById(R.id.line);
        mViewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
        textView[0]=mTv_expert= (TextView) rootView.findViewById(R.id.Tv_expert);
        textView[1]=mTv_cat= (TextView) rootView.findViewById(R.id.Tv_cat);

       // settingFragment = new SettingFragment();
    }

    private int mScreenW;
    private void initLine() {
        mScreenW = PixelUtil.getScreenWidth(getActivity()); // 屏幕宽度
        int w = PixelUtil.dp2px(getActivity(), 2);
        mLine.setLayoutParams(new LinearLayout.LayoutParams(mScreenW / 2, w));
    }

    /**
     * 字体颜色变化：选中时是白色，不选择的是灰色
     *
     * @param position
     */
    private void setTabSelect(int position) {
        for (int i = 0; i < textView.length; i++) {
            if (position == i) {
                textView[i].setTextColor(getResources().getColor(R.color.white));
            } else {
                textView[i].setTextColor(getResources().getColor(R.color.rose_hui));
            }
        }
    }
    /**
     * 红线
     * @param index
     */
    private void fixTab(int index) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mLine.getLayoutParams();//红线
        lp.leftMargin = (int) ((mScreenW / 2) * index);//红线
        mViewPager.setCurrentItem(index, false);
        mLine.setLayoutParams(lp);
        if (index==0){
            EventBus.getDefault().post(new UserEvent(UserEvent.SCEOLL_TO_TOP));
        }
    }

    private Fragment fragments[];

    private void init(){
        videoFragment=new VideoFrament();
        catFragment=new AttentionFragement();
        fragments= new Fragment[]{videoFragment,catFragment};
        mViewPager.setAdapter(new MyPagerAdapter(getFragmentManager(), fragments));
        mViewPager.setOffscreenPageLimit(5);
        setTabSelect(0);//初测创建的时候;
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                index = position;
                setTabSelect(position);
                fixTab(index);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
    /**
     * 每个Linearlayout的点击事件
     */
    public void onTabClicked(View view) {
        int tamindex = 0;
        switch (view.getId()) {
            case R.id.Lin_expert:
                tamindex = 0;
                break;
            case R.id.Lin_cat:
                tamindex = 1;
                break;
        }
        fixTab(tamindex);
    }
}
